//
//  IconContext.m
//  DeallocTest
//
//  Created by 冷悦 on 16/3/24.
//  Copyright © 2016年 冷悦. All rights reserved.
//

#import "IconContext.h"
#import "PlistManager.h"
#import "NetworkingBaseManager.h"
#import "NetworkingManagerFactory.h"
#import "MJExtension.h"
#import "UIAlertControllerUtil.h"
#import "RequestGenerator.h"
//#import "AppDelegate+ConfigHXAndIcon.h"

#ifndef kIconContextFileName
#define kIconContextFileName @"icon"
#endif

@interface IconContext () <NetworkingBaseManagerAPISuccessCallbackDelegate,NetworkingBaseManagerValidator>

@property (nonatomic, strong)NetworkingBaseManager *loadManager;

@property (nonatomic, strong)NetworkingBaseManager *checkManager;

@end

@implementation IconContext

static IconContext *_iconContext = nil;

#pragma mark - Initialize
+ (instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        _iconContext = [[self alloc] init];
        
        if ([PlistManager isExistWithFileName:kIconContextFileName]) {
            NSDictionary *iconDict = [_iconContext loadIconFromFile];
             _iconContext.iconVersion = [[iconDict objectForKey:@"iconVersion"] integerValue];
            [_iconContext mj_setKeyValues:[iconDict objectForKey:@"icons"]];
            [_iconContext checkIcons:_iconContext.iconVersion];
        } else {
            [_iconContext loadIcons];
        }
    });
    return _iconContext;
}

#pragma mark - NetworkingBaseManager
- (void)manager:(NetworkingBaseManager *)manager callAPIDidSuccessWithParams:(NSDictionary *)responseParams{
    
    if (manager == self.loadManager) {
        
        NSDictionary *iconDict = [responseParams objectForKey:@"data"];
        [self mj_setKeyValues:[iconDict objectForKey:@"icons"]];
        _iconVersion = [[iconDict objectForKey:@"iconVersion"] integerValue];
        [self saveIconToPlist:iconDict];
//         [[NSNotificationCenter defaultCenter] postNotificationName:XNotification_AutoLogin object:nil];
    }
    
    if (manager == self.checkManager){
        
        NSDictionary *infoDict = [responseParams objectForKey:@"data"];
        BOOL keepIcon = [[infoDict objectForKey:@"result"] boolValue];
        
        if (keepIcon == NO) {
            [self loadIcons];
        }else{
//          [[NSNotificationCenter defaultCenter] postNotificationName:XNotification_AutoLogin object:nil];
        }
    }
}

#pragma mark - Delegate NetworkingBaseManagerValidator
- (BOOL)isCorrectWithCallbackData:(NSDictionary *)data{
    NSInteger stateCode = [[data objectForKey:@"stateCode"] integerValue];
    if (stateCode == 100||stateCode == 101|| stateCode == 102|| stateCode == 103) {
        return YES;
    }else{
        [UIAlertControllerUtil generateInfoControllerWithTitle:[NSString stringWithFormat:@"%ld",stateCode] message:[data objectForKey:@"stateMessage"]];
        return NO;
    }
}

#pragma mark - Public
- (void)loadIcons{
    self.loadManager = [NetworkingManagerFactory generateNormalManager];
    self.loadManager.requestType = HTTP_GET;
    NSMutableDictionary *postDic = [NSMutableDictionary dictionary];
    [postDic setObject:[NSNumber numberWithBool:!iPhone5] forKey:@"isLargePhoneSize"];
    self.loadManager.params = postDic;
    self.loadManager.successDelegate = self;
    self.loadManager.validator = self;
    self.loadManager.apiName = [[RequestGenerator sharedInstance] generateCommonApiWithName:@"softwareIcons"];
    [self.loadManager loadRequest];
}

- (NSString *)iconByName:(NSString *)iconName{
    NSString *iconPath;
    
    if (iconName != nil) {
        iconPath = [NSString stringWithFormat:@"%@/%@%@", kBaseUrl, kAppVersion, iconName];
    }
    
    return iconPath;
}

#pragma mark private
//将接收到的图标保存到plist文件中
- (void)saveIconToPlist:(NSDictionary *)iconDict{
    [PlistManager saveData:iconDict withFileName:kIconContextFileName];
}

//将plist文件中保存的图标转换为NSDictionary
- (NSDictionary *)loadIconFromFile{
    NSDictionary *iconDict = [PlistManager loadDataWithFileName:kIconContextFileName];
    return iconDict;
}

- (void)checkIcons:(NSUInteger)iconVersion{
    self.checkManager = [NetworkingManagerFactory generateNormalManager];
    self.checkManager.requestType = HTTP_GET;
    self.checkManager.successDelegate = self;
    self.checkManager.validator = self;
    NSString *apiName = [[RequestGenerator sharedInstance] generateCommonApiWithName:[NSString stringWithFormat:@"softwareIcons/%lu",iconVersion]];
    self.checkManager.apiName = apiName;
    [self.checkManager loadRequest];
}


@end
